package cn.hd.service.impl;

import java.util.Date;
import cn.hd.entity.User;
import cn.hd.dao.CommentDao;
import cn.hd.dao.UserDao;
import cn.hd.entity.Comment;
import cn.hd.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<Comment> selectAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> comment = commentDao.selectAll();
        PageInfo<Comment>  page = new PageInfo<>(comment);
        return page;
    }

    @Override
    public PageInfo<Comment> selectAllByPid(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> comments = commentDao.selectAllByPid(map);
        PageInfo<Comment> page = new PageInfo<>(comments);
        return page;

    }

    @Override
    public int addComment(Comment comment,long id) {
        //根据id查询用户名称
        User user = userDao.selectUserById(id);
        //把用户名称放到comment里面
        comment.setCname(user.getUsername());
        Date date = new Date();
        comment.setCtime(date);
        return commentDao.addComment(comment);
    }

    @Override
    public int addUserComment(long uid, long cid,long pid) {
        return commentDao.addUserComment(uid,cid,pid);
    }

    @Override
    public List<Comment> selectCommentAllsrp(long pid) {
        List<Comment> comments = commentDao.selectCommentAllsrp(pid);
        return comments;
    }

    @Override
    public int deleteComment(long cid) {
        return commentDao.deleteComment(cid);
    }

    @Override
    public int deleteCommentUser(long cid) {
        return commentDao.deleteCommentUser(cid);
    }

    @Override
    public int updateComment(Comment comment) {
        return commentDao.updateComment(comment);
    }
}
